package edu.nf.exam.dao.impl;

import com.github.pagehelper.PageInfo;
import edu.nf.exam.dao.SubjectInfoDao;
import edu.nf.exam.entity.SubjectInfo;
import edu.nf.exam.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author : QYM
 * @create 2021/6/17
 *  学科信息表--接口实现类
 */
public class SubjectInfoDaoImpl implements SubjectInfoDao {
    /**
     * 添加学科
     * @param subjectInfo
     * @return
     */
    @Override
    public int addSubject(SubjectInfo subjectInfo) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        int r = sqlSession.getMapper(SubjectInfoDao.class).addSubject(subjectInfo);
        sqlSession.close();
        return r;
    }

    /**
     * 删除学科
     * @param subjectId
     * @return
     */
    @Override
    public int delSubject(Integer subjectId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        int r = sqlSession.getMapper(SubjectInfoDao.class).delSubject(subjectId);
        sqlSession.close();
        return r;
    }

    /**
     * 修改学科
     * @param subjectInfo
     * @return
     */
    @Override
    public int updateSubject(SubjectInfo subjectInfo) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        int r = sqlSession.getMapper(SubjectInfoDao.class).updateSubject(subjectInfo);
        sqlSession.commit();
        sqlSession.close();
        return r;
    }

    /**
     * 模糊查询学科
     * @param levelName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<SubjectInfo> queryByLikeSubject(String levelName, Integer pageNum, Integer pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        List<SubjectInfo> list = sqlSession.getMapper(SubjectInfoDao.class).queryByLikeSubject(levelName,pageNum,pageSize);
        sqlSession.close();
        return list;
    }

    /**
     * 根据ID查询学科信息
     * @param subjectId
     * @return
     */
    @Override
    public SubjectInfo queryByIdSubject(Integer subjectId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        SubjectInfo subject = sqlSession.getMapper(SubjectInfoDao.class).queryByIdSubject(subjectId);
        sqlSession.close();
        return subject;
    }

    /**
     * 去重查询年级
     * @return
     */
    @Override
    public List<SubjectInfo> selectLevelNameByDistinct() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        List<SubjectInfo> list = sqlSession.getMapper(SubjectInfoDao.class).selectLevelNameByDistinct();
        sqlSession.close();
        return list;
    }


    /**
     * 多条件查询
     * @param subjectInfo
     * @return
     */
    @Override
    public List<SubjectInfo> selectByMore(SubjectInfo subjectInfo) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        List<SubjectInfo> list = sqlSession.getMapper(SubjectInfoDao.class).selectByMore(subjectInfo);
        sqlSession.close();
        return list;
    }

    /**
     * 根据学科名称和年级查询，服务于判断学科创编和修改是否唯一
     * @param subjectName
     * @param levelName
     * @return
     */
    @Override
    public int queryByNameSubject(String subjectName,String levelName) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        int count = sqlSession.getMapper(SubjectInfoDao.class).queryByNameSubject(subjectName,levelName);
        sqlSession.close();
        return count;
    }
}
